<?php
/**
 * 接口配置
 * Enter description here ...
 * @author jin
 *
 */
class ControllerConfigReply extends Controller {
	private $error = array();

	protected function init(){
		$this->load_language('config/reply');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('config/reply');
	}

	public function index() {
		$this->init();
		$this->getList();
	}
	

	public function insert() {
		$this->init();

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			$this->model_config_keyword->addKeyword($this->request->post);

			$this->redirectToList();
		}

		$this->getForm();
	}

	public function update() {
		$this->init();

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			$this->model_config_reply->editReply($this->request->get['keyword_id'], $this->request->post);

			$this->redirectToList();
		}

		$this->getForm();
	}

	public function delete() {
		$this->init();

		if (isset($this->request->post['selected']) && $this->validateDelete()) {
			foreach ($this->request->post['selected'] as $postion_id) {
				$this->model_config_position->deletePosition($postion_id);
			}

			$this->redirectToList();
		}

		$this->getList();
	}
	 
	 
	private function getList() {
		
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}

		$data = array(
			'start' => ($page - 1) * $this->config->get('config_admin_limit'),
			'limit' => $this->config->get('config_admin_limit')
		);


		$this->data['replys'] = array();

		$user_total = $this->model_config_reply->getTotalReplys();

		$results = $this->model_config_reply->getReplys($data);

		foreach ($results as $result) {
			$action = array();

			$action[] = array(
				'text' => $this->language->get('text_edit'),
				'type' => "edit",
				'href' => $this->url->link('config/reply/update', 'token=' . $this->session->data['token'] . '&keyword_id=' . $result['keyword_id'] , 'SSL')
			);


			$this->data['keywords'][] = array(
				'reply_id'    => $result['reply_id'],
				'keyword_id'    => $result['keyword_id'],
      			'content'    => $result['content'],
      			'status'     => ($result['status'] ? $this->language->get('text_enabled') : $this->language->get('text_disabled')),
				'actions'     => $action,
      			'selected'   => isset($this->request->post['selected']) && in_array($result['reply_id'], $this->request->post['selected']),
			);
		}

		$url = "";

		if(isset($this->request->get['reply_id']))
		{
			$url = "&reply_id="+$this->request->get['reply_id'];
		}


		$this->data['insert'] = $this->url->link('config/reply/insert', 'token=' . $this->session->data['token'], 'SSL');
		$this->data['delete'] = $this->url->link('config/reply/delete', 'token=' . $this->session->data['token'].$url, 'SSL');
		
		$this->data['token'] = $this->session->data['token'];
		$pagination = new Pagination();
		$pagination->total = $user_total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_admin_limit');
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = $this->url->link('config/reply', 'token=' . $this->session->data['token'] . '&page={page}', 'SSL');
		$this->data['pagination'] = $pagination->render();
		
		$this->template = 'config/reply_list.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
		$this->response->setOutput($this->render());
	}

	private function getForm() {
		
		
		if (isset($this->error['title'])) {
			$this->data['error_title'] = $this->error['title'];
		} else {
			$this->data['error_title'] = '';
		}

		if (isset($this->error['error_date'])) {
			$this->data['error_date'] = $this->error['error_date'];
		} else {
			$this->data['error_date'] = '';
		}

		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

		$url=$this->getUrlParameters();

		$this->data['breadcrumbs'] = array();

		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
		);

		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('config/keyword', 'token=' . $this->session->data['token'] , 'SSL'),
      		'separator' => ' :: '
      		);

      		if (!isset($this->request->get['keyword_id'])) {
      			$this->data['action'] = HTTPS_SERVER . 'index.php?route=config/reply/insert&token=' . $this->session->data['token'] . $url;
      		} else {
      			$this->data['action'] = HTTPS_SERVER . 'index.php?route=config/reply/update&token=' . $this->session->data['token'] . '&keyword_id=' . $this->request->get['keyword_id'] . $url;
      		}

      		$this->data['cancel'] = HTTPS_SERVER . 'index.php?route=config/keyword&token=' . $this->session->data['token'] . $url;

      		$this->data['token'] = $this->session->data['token'];

      		if (isset($this->request->get['keyword_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
      			$reply_info = $this->model_config_reply->getReply($this->request->get['keyword_id']);
      		}
      		
			if (isset($this->request->post['content'])) {
				$this->data['content'] = $this->request->post['content'];
			} elseif (!empty($reply_info)) {
				$this->data['content'] = $reply_info['content'];
			} else {
				$this->data['content'] = '';
			}
			
			if (isset($this->request->post['keyword_id'])) {
				$this->data['keyword_id'] = $this->request->post['keyword_id'];
			} elseif (!empty($reply_info)) {
				$this->data['keyword_id'] = $reply_info['keyword_id'];
			} else {
				if(isset($this->request->get['keyword_id'])){
					$this->data['keyword_id'] = (int)$this->request->get['keyword_id'];
				}else{
					$this->data['keyword_id'] = '';
				}
			}
			
      		$this->load->model('config/keyword');
			$keywords = $this->model_config_keyword->getAllKeywords();
			$this->data['keywords'] = $keywords;
      		
      		$this->template = 'config/reply_form.tpl';
			$this->children = array(
				'common/header',
				'common/footer'
			);
			$this->response->setOutput($this->render());
      		
	}
	
	
	public function example(){
		$this->template = 'config/config_position_example.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
		$this->response->setOutput($this->render());
	}

	private function validateForm() {
		/*
		if (!$this->user->hasPermission('modify', 'catalog/article')) {
			$this->error['warning'] = sprintf($this->language->get('error_permission'),$this->language->get('heading_title'));
		}

		foreach ($this->request->post['article'] as $language_id => $value) {
			if ((strlen(utf8_decode($value['title'])) < 1)) {
				$this->error['title'][$language_id] = $this->language->get('error_name');
			}
		}
		 */
		if (!$this->error) {
			return TRUE;
		} else {
			$this->error['warning'] = $this->language->get('error_required_data');
				
			return FALSE;
		}
	}

	private function validateDelete() {
		if (!$this->user->hasPermission('modify', 'catalog/article')) {
			$this->error['warning'] = sprintf($this->language->get('error_permission'),$this->language->get('heading_title'));
		}

		if (!$this->error) {
			return TRUE;
		} else {
			return FALSE;
		}
	}

	public function category() {
		$this->load->model('catalog/article');

		if (isset($this->request->get['category_id'])) {
			$category_id = $this->request->get['category_id'];
		} else {
			$category_id = 0;
		}

		$article_data = array();

		$results = $this->model_catalog_article->getArticleByCategoryId($category_id);

		foreach ($results as $result) {
			$article_data[] = array(
				'article_id' => $result['article_id'],
				'title'       => $result['name']
			);
		}

		$this->load->library('json');

		$this->response->setOutput(Json::encode($article_data));
	}

	public function related() {
		$this->load->model('catalog/article');

		if (isset($this->request->post['article_related'])) {
			$article = $this->request->post['article_related'];
		} else {
			$article = array();
		}

		$article_data = array();

		foreach ($article as $article_id) {
			$article_info = $this->model_catalog_article->getArticleName($article_id);

			if ($article_info) {
				$article_data[] = array(
					'article_id' => $article_info['article_id'],
					'title'       => $article_info['name']
				);
			}
		}

		$this->load->library('json');

		$this->response->setOutput(Json::encode($article_data));
	}

	private function redirectToList(){
		$this->session->data['success'] = sprintf($this->language->get('text_success'),$this->language->get('heading_title'));

		$url=$this->getUrlParameters();

		$this->redirect(HTTPS_SERVER . 'index.php?route=config/keyword&token=' . $this->session->data['token'] . $url);
	}
	 
	private function getCommonUrlParameters(){
		$url = '';

		if (isset($this->request->get['filter_title'])) {
			$url .= '&filter_title=' . $this->request->get['filter_title'];
		}

		if (isset($this->request->get['filter_status'])) {
			$url .= '&filter_status=' . $this->request->get['filter_status'];
		}

		if (isset($this->request->get['filter_date_add'])) {
			$url .= '&filter_date_add=' . $this->request->get['filter_date_add'];
		}

		if (isset($this->request->get['filter_article_category_id'])) {
			$url .= '&filter_article_category_id=' . $this->request->get['filter_article_category_id'];
		}

		return $url;
	}
	 
	public function getUrlParameters(){
		$url = '';

		$url=$this->getCommonUrlParameters();

		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}

		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}

		return $url;
	}

}
?>